package com.netty.server;

import com.common.protobuf.MessageModule;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.stereotype.Component;

/**
 * @author 顾志杰
 * @date 2021/3/12-10:30
 */
@Component
public class Server {
    public Server(){
        //配置服务器端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();

        try{
           b.group(bossGroup,workerGroup)
           .channel(NioServerSocketChannel.class)
           .option(ChannelOption.SO_BACKLOG,1024)
           .handler(new LoggingHandler(LogLevel.INFO))
           .childHandler(new ChannelInitializer<SocketChannel>() {
               @Override
               protected void initChannel(SocketChannel ch) throws Exception {
                   ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                   ch.pipeline().addLast(new ProtobufDecoder(MessageModule.Message.getDefaultInstance()));
                   ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                   ch.pipeline().addLast(new ProtobufEncoder());
                   ch.pipeline().addLast(new ServerHandler());
               }
           });
           // 绑定端口，同步等待成功
            ChannelFuture cf = b.bind(8765).sync();
            System.out.println(" Server startup.. prot: 8765");
            cf.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            System.out.println(" Server shutdown.. ");
        }
    }
}
